<template>
  <div class="app-container">
    <!-- 按钮组 -->
    <el-row style="padding-top:10px;">
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button-group>
            <el-button type="primary" @click="addClk">新增</el-button>
            <el-button type="success">高级搜索</el-button>
          </el-button-group>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <div>
            <el-input placeholder="请输入内容" @blur="searchBlur" v-model="searchVal" class="input-with-select">
              <el-button slot="append" @click="searchClk" icon="el-icon-search"></el-button>
            </el-input>
          </div>
        </div>
      </el-col>
    </el-row>
    <!-- 表格 -->
    <template>
      <!-- @sort-change="sortChange" -->
      <el-table
        max-height="400"
        :data="listArr"
        border
        style="width: 100%;margin:10px auto;"
      >
        <el-table-column fixed type="index" label="#" width="50"></el-table-column>
        <el-table-column prop="title" label="题目"></el-table-column>
        <el-table-column prop="type" label="分类" width="80"></el-table-column>
        <el-table-column prop="name" label="作者" width="80"></el-table-column>
        <el-table-column prop="date" label="发布时间" sortable></el-table-column>
        <el-table-column fixed="right" label="操作">
          <template slot-scope="scope">
            <el-button type="info" size="small" @click="lookClk(scope.row)">查看</el-button>
            <el-button type="warning" size="small" @click="updateClk(scope.row)">修改</el-button>
            <el-button type="danger" size="small" @click="deleteClk(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <!-- 抽屉 -->
    <el-drawer
      size="50%"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
    >
      <!-- form -->
      <el-form ref="form" size="mini" style="padding:0 5%;" :model="form" label-width="80px">
        <el-form-item label="题目">
          <el-input v-model="form.title" rows="1" type="textarea" :disabled="look" placeholder="测试题目"></el-input>
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="form.cid" :disabled="look" placeholder="请选择类别">
            <el-option
              v-for="(item,index) in classify"
              :key="index"
              :value="item.id"
              :label="item.name"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="选项">
          <el-checkbox-group class="checkBoxs" v-model="form.answers" :disabled="look">
            <label v-for="(item,index) in form.options" :key="index">
              <el-checkbox :checked="item.checked" :label="item.index"></el-checkbox>
              <el-input v-model="item.value" :disabled="look" rows="1" type="textarea"></el-input>
            </label>
            <div class="btns">
              <el-button @click="addOption" :disabled="look" type="success" icon="el-icon-plus" circle></el-button>
              <el-button @click="delOption" :disabled="look" v-show="form.options.length != 4" type="danger" icon="el-icon-minus" circle></el-button>
            </div>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="答案">
          {{form.answers}}
        </el-form-item>
        <el-form-item label="答案提示">
          <el-input v-model="form.tips" :disabled="look" rows="1" type="textarea" placeholder="答案提示"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :disabled="look" @click="onSubmit">确定</el-button>
          <el-button @click="close">取消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 8, 10]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
  </div>
</template>

<script>
import { mapActions, mapState, mapMutations, mapGetters } from "vuex";
export default {
  data() {
    return {
      drawer: false,
      form: {
        answers: [],
        options: [
          {
            index: "A",
            value: "",
            checked: false
          },
          {
            index: "B",
            value: "",
            checked: false
          },
          {
            index: "C",
            value: "",
            checked: false
          },
          {
            index: "D",
            value: "",
            checked: false
          }
        ]
      },
      idArr: [],
      searchVal: "",
      look: false,
    };
  },
  mounted() {
    this.getList();
    this.$store.dispatch("classify/getlist")
  },
  methods: {
    ...mapActions("exam", ["getList", "addList", "updateList", "delList"]),
    ...mapMutations("exam", ["SET_CURR", "SET_PAGE", "SET_SEARCH"]),  // "SET_SORT"
    addOption(){
      let word = String.fromCharCode(this.form.options.length + 65);
      this.form.options.push({ index: word, value: "", checked: false });
    },
    delOption(){
      this.form.options.pop();
    },
    lookClk(row) {
      this.look = true;
      this.drawer = true;
      this.form = { ...row };
      this.form.answers = JSON.parse(this.form.answers)
      this.form.options = JSON.parse(this.form.options)
    },
    searchBlur(){
      this.searchClk()
    },
    searchClk() {
      this.SET_SEARCH(this.searchVal);
      this.getList();
    },
    deleteClk(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.delList(id);
          this.$message({
            type: "success",
            message: "删除成功!"
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    updateClk(row) {
      this.look = false;
      this.form = { ...row };
      this.form.answers = JSON.parse(this.form.answers)
      this.form.options = JSON.parse(this.form.options)
      this.drawer = true;
    },
    addClk() {
      this.look = false;
      this.drawer = true;
    },
    close() {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.drawer = false;
          this.form = {
            answers: [],
            options: [
              {
                index: "A",
                value: "",
                checked: false
              },
              {
                index: "B",
                value: "",
                checked: false
              },
              {
                index: "C",
                value: "",
                checked: false
              },
              {
                index: "D",
                value: "",
                checked: false
              }
            ]
          };
        })
        .catch(_ => {});
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          done();
        })
        .catch(_ => {});
    },
    onSubmit() {
      if (this.form.id) {
        this.updateList(this.form);
      } else {
        this.addList({
          form: this.form,
          uid: this.$store.state.user.uid
        });
      }
      this.drawer = false;
      this.form = {
        answers: [],
        options: [
          {
            index: "A",
            value: "",
            checked: false
          },
          {
            index: "B",
            value: "",
            checked: false
          },
          {
            index: "C",
            value: "",
            checked: false
          },
          {
            index: "D",
            value: "",
            checked: false
          }
        ]
      };
    },
    handleSizeChange(val) {
      this.SET_PAGE(val);
      this.getList();
    },
    handleCurrentChange(val) {
      this.SET_CURR(val);
      this.getList();
    }
  },
  computed: {
    ...mapState("exam", [
      "listArr",
      "total",
      "currentPage",
      "pageSize",
    ]),
    ...mapState('classify',["classify"])
  }
};
</script>

<style scoped>
.zzc {
  z-index: 9999;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.3);
}

.checkBoxs {
  max-width: 350px;
  position: relative;
  margin-right: 80px;
  padding-right: 10px;
}

.checkBoxs label {
  margin-bottom: 10px;
  display: flex;
  align-items: center;
}

.checkBoxs label .el-checkbox {
  margin-bottom: 0;
}

.checkBoxs label >>> .el-input--mini {
  margin-left: 10px;
}

.btns {
  display: flex;
  position: absolute;
  bottom: 0;
  left: 100%;
}
</style>